﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using System.IO.Ports;


namespace 激光与气象仪界面集成
{
    public partial class Form2 : Form
    {
        private Form1 paf;
        public Form2(Form1 parent)
        {
            InitializeComponent();
            paf = parent;
            //this.chart1 = new Chart();
           // this.Controls.Add(this.chart1); this.Controls.Add(this.chart2);
            // 设置Chart属性
            //this.chart1.Dock = DockStyle.Fill;
            this.chart1.Titles.Add("德国激光测距 毫米");
            //this.chart1.ChartAreas[0].AxisY.Minimum = 0;
            //this.chart1.ChartAreas[0].AxisY.Maximum = 305;

            //this.chart2.ChartAreas[0].AxisY.Minimum = 0;
            //this.chart2.ChartAreas[0].AxisY.Maximum = 20;

            // 设置Chart属性
            //this.chart2.Dock = DockStyle.Fill;
            this.chart2.Titles.Add("气象风速 米/秒");

            Axis xAxis = new Axis();

            // 设置X轴的名称
            xAxis.Title = "时间数量5秒一格";

            // 将X轴添加到Chart控件中
           // chart1.ChartAreas[0].AxisX = xAxis;
            chart2.ChartAreas[0].AxisX = xAxis;

            Axis y1Axis = new Axis();
            // 设置X轴的名称
            y1Axis.Title = "距离范围80-300mm";
            Axis y2Axis = new Axis();
            // 设置X轴的名称
            y2Axis.Title = "风速最大30m/s";

            chart1.ChartAreas[0].AxisY = y1Axis;
            chart2.ChartAreas[0].AxisY = y2Axis;

            this.chart1.ChartAreas[0].AxisY.Minimum = 80.00;
            this.chart1.ChartAreas[0].AxisY.Maximum = 305.00;


            this.chart2.ChartAreas[0].AxisY.Minimum = 0.00;
            this.chart2.ChartAreas[0].AxisY.Maximum = 30.00;

            try
                { HistoryCSVDateShow(); }
                catch
                {
                    MessageBox.Show("被选日期的历史数据不存在！");
                }
            

        }
        public List<string> Date_x_Mouse = new List<string>();
        public void HistoryCSVDateShow()//参数读取
        {

            //****//绝对地址// string csvFile = System.IO.Path.Combine(Application.StartupPath, @"C:\Users\Administrator\Desktop\log.csv");//************************************///这里是历史数据保存的地址，随便改啊//************************************//
            //string csvFile = System.IO.Path.Combine(Application.StartupPath, Path.GetFullPath(txtPath.Text));
            string csvFile = System.AppDomain.CurrentDomain.BaseDirectory + "激光测距和微气象仪历史数据\\"  + "激光测距和微气象仪历史数据"+ paf.dateTimePicker1.Text+".csv";
            label1.Text = paf.dateTimePicker1.Text;
            List<string[]> rows = File.ReadAllLines(csvFile).Select(x => x.Split(',')).ToList();
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("距离1 mm");
            dataTable.Columns.Add("距离2 mm");
            dataTable.Columns.Add("距离3 mm");
            dataTable.Columns.Add("距离4 mm");
            dataTable.Columns.Add("风速1 m/s");
            dataTable.Columns.Add("风向1 °");
            dataTable.Columns.Add("风速2 m/s");
            dataTable.Columns.Add("风向2 °");
            dataTable.Columns.Add("校准距离1");
            dataTable.Columns.Add("校准距离2");
            dataTable.Columns.Add("校准距离3");
            dataTable.Columns.Add("校准距离4");
            dataTable.Columns.Add("阈值1");
            dataTable.Columns.Add("阈值2");
            dataTable.Columns.Add("阈值3");
            dataTable.Columns.Add("阈值4");
            dataTable.Columns.Add("日期");
            dataTable.Columns.Add("备注");
            rows.ForEach(x => { dataTable.Rows.Add(x); });

            int dataTableRowsNum = dataTable.Rows.Count;
            List<double> timeShaft = new List<double>();
            List<double> Laser1 = new List<double>();
            List<double> Laser2 = new List<double>();
            List<double> Laser3 = new List<double>();
            List<double> Laser4 = new List<double>();
            List<double> WindSpeed1 = new List<double>();
            List<double> WindSpeed2 = new List<double>();
            List<string> Date = new List<string>();
            Date_x_Mouse = Date;
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();
            chart1.Series[2].Points.Clear();
            chart1.Series[3].Points.Clear();
            chart2.Series[0].Points.Clear();
            chart2.Series[1].Points.Clear();
            
            chart1.ChartAreas[0].CursorY.LineWidth = 1; 
            
            chart2.ChartAreas[0].CursorY.LineWidth = 1;
            for (int i = 0; i < (dataTableRowsNum - 1); i++)
            {
                try
                {
                    timeShaft.Add(i);
                    Laser1.Add(Convert.ToDouble(dataTable.Rows[i][0]));
                    Laser2.Add(Convert.ToDouble(dataTable.Rows[i][1]));
                    Laser3.Add(Convert.ToDouble(dataTable.Rows[i][2]));
                    Laser4.Add(Convert.ToDouble(dataTable.Rows[i][3]));
                    WindSpeed1.Add(Convert.ToDouble(dataTable.Rows[i][4]));
                    WindSpeed2.Add(Convert.ToDouble(dataTable.Rows[i][6]));
                    Date.Add(dataTable.Rows[i][16].ToString());
                }
                catch { }
            }
            for (int i = 0; i < dataTableRowsNum - 1; i++)
            {
                try
                {
                    chart1.Series[0].Points.AddXY(timeShaft[i], Laser1[i]);
                    chart1.Series[1].Points.AddXY(timeShaft[i], Laser2[i]);
                    chart1.Series[2].Points.AddXY(timeShaft[i], Laser3[i]);
                    chart1.Series[3].Points.AddXY(timeShaft[i], Laser4[i]);

                    chart2.Series[0].Points.AddXY(timeShaft[i], WindSpeed1[i]);
                    chart2.Series[1].Points.AddXY(timeShaft[i], WindSpeed2[i]);

                    //chart1.Series[0].AxisLabel = "大笨蛋";
                    String[] Right, Left;
                    int hourR, minuR, hourL, minuL;
                    Right = Date[i].Split('日');
                    Left = Date[i - 1].Split('日');
                    Right = Right[1].Split('时');
                    Left = Left[1].Split('时');
                    hourR = Convert.ToInt32(Right[0]);
                    hourL = Convert.ToInt32(Left[0]);
                    Right = Right[1].Split('分');
                    Left = Left[1].Split('分');
                    minuR = Convert.ToInt32(Right[0]);
                    minuL = Convert.ToInt32(Left[0]);
                    if (Math.Abs(hourR*60- hourL*60+ minuR- minuL) > 10)
                    {
                        if (checkBox1.CheckState == CheckState.Checked)
                        {
                            chart1.Series[0].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart1.Series[0].Points[i].MarkerColor = Color.Red;
                            chart1.Series[0].Points[i].MarkerBorderWidth = 3;
                            chart1.Series[0].Points[i].MarkerSize = 10;
                            chart1.Series[0].Points[i].Label = "值:" + Laser1[i].ToString() + " 时间:" + Date[i].ToString();
                            chart1.Series[0].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart1.Series[0].Points[i].MarkerStyle = MarkerStyle.None;
                            chart1.Series[0].Points[i].Label = "";
                            chart1.Series[0].Points[i].IsValueShownAsLabel = false;
                        }
                        if (checkBox2.CheckState == CheckState.Checked)
                        {
                            chart1.Series[1].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart1.Series[1].Points[i].MarkerColor = Color.Red;
                            chart1.Series[1].Points[i].MarkerBorderWidth = 3;
                            chart1.Series[1].Points[i].MarkerSize = 10;
                            chart1.Series[1].Points[i].Label = "值:" + Laser2[i].ToString() + " 时间:" + Date[i].ToString();
                            chart1.Series[1].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart1.Series[1].Points[i].MarkerStyle = MarkerStyle.None;
                            chart1.Series[1].Points[i].Label = "";
                            chart1.Series[1].Points[i].IsValueShownAsLabel = false;
                        }
                        if (checkBox3.CheckState == CheckState.Checked)
                        {
                            chart1.Series[2].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart1.Series[2].Points[i].MarkerColor = Color.Red;
                            chart1.Series[2].Points[i].MarkerBorderWidth = 3;
                            chart1.Series[2].Points[i].MarkerSize = 10;
                            chart1.Series[2].Points[i].Label = "值:" + Laser3[i].ToString() + " 时间:" + Date[i].ToString();
                            chart1.Series[2].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart1.Series[2].Points[i].MarkerStyle = MarkerStyle.None;
                            chart1.Series[2].Points[i].Label = "";
                            chart1.Series[2].Points[i].IsValueShownAsLabel = false;
                        }
                        if (checkBox4.CheckState == CheckState.Checked)
                        {
                            chart1.Series[3].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart1.Series[3].Points[i].MarkerColor = Color.Red;
                            chart1.Series[3].Points[i].MarkerBorderWidth = 3;
                            chart1.Series[3].Points[i].MarkerSize = 10;
                            chart1.Series[3].Points[i].Label = "值:" + Laser4[i].ToString() + " 时间:" + Date[i].ToString();
                            chart1.Series[3].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart1.Series[3].Points[i].MarkerStyle = MarkerStyle.None;
                            chart1.Series[3].Points[i].Label = "";
                            chart1.Series[3].Points[i].IsValueShownAsLabel = false;
                        }
                        if (checkBox5.CheckState == CheckState.Checked)
                        {
                            chart2.Series[0].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart2.Series[0].Points[i].MarkerColor = Color.Red;
                            chart2.Series[0].Points[i].MarkerBorderWidth = 3;
                            chart2.Series[0].Points[i].MarkerSize = 10;
                            chart2.Series[0].Points[i].Label = "值:" + WindSpeed1[i].ToString() + " 时间:" + Date[i].ToString();
                            chart2.Series[0].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart2.Series[0].Points[i].MarkerStyle = MarkerStyle.None;
                            chart2.Series[0].Points[i].Label = "";
                            chart2.Series[0].Points[i].IsValueShownAsLabel = false;
                        }
                        if (checkBox6.CheckState == CheckState.Checked)
                        {
                            chart2.Series[1].Points[i].MarkerStyle = MarkerStyle.Diamond;
                            chart2.Series[1].Points[i].MarkerColor = Color.Red;
                            chart2.Series[1].Points[i].MarkerBorderWidth = 3;
                            chart2.Series[1].Points[i].MarkerSize = 10;
                            chart2.Series[1].Points[i].Label = "值:" + WindSpeed2[i].ToString() + " 时间:" + Date[i].ToString();
                            chart2.Series[1].Points[i].IsValueShownAsLabel = true;
                        }
                        else
                        {
                            chart2.Series[1].Points[i].MarkerStyle = MarkerStyle.None;
                            chart2.Series[1].Points[i].Label = "";
                            chart2.Series[1].Points[i].IsValueShownAsLabel = false;
                        }
                    }
                }

                catch { }

            }
            /*
            double rec;
            rec = Convert.ToDouble(dataTable.Rows[1][0]);
            label1.Text= rec.ToString();
            List<double> xValues1 = new List<double>() { 0, 1, 2, 3, 4 };
            List<double> yValues1 = new List<double>() { Convert.ToDouble(dataTable.Rows[1][0]), Convert.ToDouble(dataTable.Rows[1][1]), Convert.ToDouble(dataTable.Rows[1][2]), Convert.ToDouble(dataTable.Rows[1][3]), Convert.ToDouble(dataTable.Rows[1][4]) };
            //List<double> yValues1 = new List<double>() { 2, 3, 4, 6, 7 };

            List<double> xValues2 = new List<double>() { 0, 1, 2, 3, 4 };
            List<double> yValues2 = new List<double>() { 6, 8, 10, 7, 9 };

            List<double> xValues3 = new List<double>() { 0, 1, 2, 3, 4 };
            List<double> yValues3 = new List<double>() { 7, 9, 11, 8, 10 };

            List<double> xValues4 = new List<double>() { 0, 1, 2, 3, 4 };
            List<double> yValues4 = new List<double>() { 8, 10, 12, 9, 11 };

            // 为每个数据点添加到Series中
            for (int i = 0; i < xValues1.Count; i++)
            {
                chart1.Series[0].Points.AddXY(xValues1[i], yValues1[i]);
                chart1.Series[0].AxisLabel = "大笨蛋";
                chart1.Series[1].Points.AddXY(xValues2[i], yValues2[i]);
                chart1.Series[2].Points.AddXY(xValues3[i], yValues3[i]);
                chart1.Series[3].Points.AddXY(xValues4[i], yValues4[i]);
            }
            */

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void chart2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            { HistoryCSVDateShow(); }
            catch
            {
                MessageBox.Show("被选日期的历史数据不存在！"+label1.Text);
            }
        }
  

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Point point = new Point(e.X, e.Y);
                chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(point, true);
                double x = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                
                int x_Int = Convert.ToInt16(x);
                // label1.Text = x_Int.ToString();
                label2.Text = Date_x_Mouse[x_Int].ToString();

                chart1.ChartAreas[0].CursorY.Interval = 0.01;
                chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
                
                label4.Text = chart1.ChartAreas[0].CursorY.Position.ToString("f2");
            }
            catch
            {
            }
        }

        private void chart2_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Point point = new Point(e.X, e.Y);
                chart2.ChartAreas[0].CursorX.SetCursorPixelPosition(point, true);
                double x = chart2.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                
                int x_Int = Convert.ToInt16(x);
                //label1.Text = x_Int.ToString();
                label3.Text = Date_x_Mouse[x_Int].ToString();

                chart2.ChartAreas[0].CursorY.Interval = 0.01;
                chart2.ChartAreas[0].CursorY.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
                label5.Text = chart2.ChartAreas[0].CursorY.Position.ToString("f2");
            }
            catch
            {
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
